<?php
class Wishlist extends Controller {

	function Wishlist()
	{
		parent::Controller();	
        $this->load->model('pageheadersmodel'); 
        $this->load->model('wishlistsmodel');     

        if (!session_check()) {
            set_referrer();
            redirect('insider', 'refresh');
        }

        $this->_run_wishlist_manager();

	}     
    
    function index()
    {
        //load page header
        $this->load->model('pageheadersmodel'); 
        $data["header"] = $this->pageheadersmodel->get_header('wishlist');
                             
        //list  
        $data["wishlists"] = $this->wishlistsmodel->get_wishlists($this->session->userdata('user_id'));

        $data["list"]["items"] = $this->wishlistsmodel->get_list($this->session->userdata('user_id')); 
        $data["list"]["controller"] = "products/view/";

        $this->load->view('website/default/head', array("document_title" => DOCUMENT_TITLE . "Wish List"));  
        $this->load->view('website/default/header');  
        $this->load->view('website/wishlist/wishlist', $data);   
        $this->load->view('website/default/footer');

    }

    function view($id = NULL)
    {

        if ($id==NULL) show_404();   

        //load page header
        $this->load->model('pageheadersmodel'); 
        $data["header"] = $this->pageheadersmodel->get_header('wishlist');
        
        //wishlist info
        $data['wishlist'] = $this->wishlistsmodel->get_wishlist_info($id, $this->session->userdata('user_id'));

        if (empty($data['wishlist'])) show_404();

        $data["wishlists"] = $this->wishlistsmodel->get_wishlists($this->session->userdata('user_id'));

        //products list  
        $data["list"]["items"] = $this->wishlistsmodel->get_list($this->session->userdata('user_id'), $id); 
        $data["list"]["controller"] = "products/view/";
        $data["list"]["image_folder"] = base_url().UPLOADS_FOLDER.PRODUCT_IMG_FOLDER;

        $this->load->view('website/default/head', array("document_title" => DOCUMENT_TITLE . "Wish List"));  
        $this->load->view('website/default/header');  
        $this->load->view('website/wishlist/wishlist', $data);   
        $this->load->view('website/default/footer');   

    }


	// Shows the overlay for adding a product to the wish list. 
    function add($product_id=NULL)
    {
		if (!is_null($product_id)) 
		{
			$user_id = $this->session->userdata('user_id');	
			$lists = $this->wishlistsmodel->get_list($user_id);
			
			if (count($lists)<2) {	// if user doesn't have multiple lists, add the product to the default list			
				
				$wishlist_id = $this->wishlistsmodel->get_default_wishlist($user_id); 
				$this->wishlistsmodel->add_product($product_id, $wishlist_id, $user_id);				
				$list = $this->wishlistsmodel->get_list($user_id, $wishlist_id);
				$data["total_items"] = count($list[0]['items']);				
				$this->_overlay_add_product_success($product_id);
								
			} else {	// if user has multiple lists, show them for selecting

				// fetch product
				$this->load->model('productsmodel');
				$data["item"] = $this->productsmodel->get_product($product_id, "id_Products, Name_Products, Introduction_Products, Main_Image");
				// fetch wishlists
				$data["wishlists"] = $this->wishlistsmodel->get_list($this->session->userdata('user_id'));
				$data['token'] = generate_token("add_to_multiple_lists",20);
				
				$this->load->view('website/wishlist/wishlist_add_product_overlay', $data);					
			}	
		}
	}
	

	
	/*
	// This is the old method, where the values came from an overlay form.
	// It supported creating a new list and selecting multiple
    function add()
    {
		
	    if(validate_token("add_to_wishlist",$this->input->post("token",TRUE))){

			$product_id = $this->input->post("product_id");
			$lists = $this->input->post("lists");
			$new_wishlist_name = $this->input->post("wishlist_name");		
			$user_id = $this->session->userdata('user_id');		
			$add_new = $this->input->post("add_new");
	
	
			if (!is_array($this->input->post("product_id"))) 
			{
				$list = array( $this->input->post("product_id") );
			}
	
			if ($add_new) {
				
				if ($new_wishlist_name == "") {
					$new_wishlist_name = "New list";
				}
				
				$new_list_id = $this->wishlistsmodel->create_new_wishlist($new_wishlist_name, $user_id);	
				$lists[] = $new_list_id;
			}
			
				
			if (is_array($lists)) {
				foreach($lists as $list_id) {
	
					$this->wishlistsmodel->add_product($product_id, $list_id, $user_id);
					
				}
			}
		}
		redirect( 'wishlist', 'refresh');
    }
	*/	
	
	// Receive the submition when a product is added to multiple lists 
    function add_product_to_multiple_lists()
	{
		$user_id = $this->session->userdata('user_id');	
		$product_id = $this->input->post('product_id');	
		$lists = (array) $this->input->post("lists");
		
		if(validate_token("add_to_multiple_lists",$this->input->post("token",TRUE)))
		{
			foreach($lists as $list_id) 
			{
				$this->wishlistsmodel->add_product($product_id, $list_id, $user_id);
			}	

			$this->_overlay_add_product_success($product_id);
		}
	}	

	// Success view after adding a product
    private function _overlay_add_product_success($product_id)
    {
		//$this->load->model('productsmodel');
		//$data["item"] = $this->productsmodel->get_product($product_id, "Name_Products, Introduction_Products, Main_Image");
		$data["wishlists"] = $this->wishlistsmodel->get_list($this->session->userdata('user_id'));
		$this->load->view('website/wishlist/wishlist_add_product_success_overlay', $data);	
	}
	
    function move()
    {
	    if(validate_token("move_to_wishlist",$this->input->post("token",TRUE))){
						
			$product_id = $this->input->post("product_id");
			$lists = $this->input->post("lists");
	
			// I don't think these lines are doing anything, they should be deleted
			if (!is_array($this->input->post("product_id"))) 
			{
				$list = array( $this->input->post("product_id") );
			}
			
			$add_new = $this->input->post("add_new");
	
			if ($add_new) {
	
				$new_wishlist_name = trim($this->input->post("wishlist_name"));	
				$user_id = $this->session->userdata('user_id');	
								
				if ($new_wishlist_name!="") {
				
					$new_list_id = $this->wishlistsmodel->create_new_wishlist($new_wishlist_name, $user_id);	
					$lists[] = $new_list_id;
				}
			}			
			
			$original_wishlist_id = $this->input->post('original_wishlist_id');		
			$user_id = $this->session->userdata('user_id');
			
			if (is_array($lists)) {
				$this->wishlistsmodel->remove_product($product_id, $original_wishlist_id, $user_id);
							
				foreach($lists as $list_id) {
	
					$this->wishlistsmodel->add_product($product_id, $list_id, $user_id);
					
				}
			}
		}

		redirect( 'wishlist', 'refresh');
    }
	
    function remove()
    {
	    if(validate_token("remove_from_wishlist",$this->input->post("token",TRUE))){
		
			$product_id = $this->input->post("product_id");
			$wishlist_id = $this->input->post("wishlist_id");
			$user_id = $this->session->userdata('user_id');
			
			$this->wishlistsmodel->remove_product($product_id, $wishlist_id, $user_id);
			
		}
		redirect( 'wishlist', 'refresh');		
    }
		
	function overlay_add_product( $product_id = NULL ) 
	{
        if ($product_id==NULL) show_404();		
		
		// fetch product data
        $this->load->model('productsmodel');
		$data["item"] = $this->productsmodel->get_product($product_id, "id_Products, Name_Products, Main_Image");        
        if (empty($data["item"])) show_404();
		
		// fetch wishlists
		$data["wishlists"] = $this->wishlistsmodel->get_wishlists($this->session->userdata('user_id'));
		
		$data['token'] = generate_token("add_to_wishlist",20);
		
		// load views
        $this->load->view('website/wishlist/wishlist_add_product_overlay', $data);
	}
	
	function overlay_move_product( $product_id = NULL, $wishlist_id = NULL ) 
	{
		// fetch product data
        if ($product_id==NULL) show_404();		
        $this->load->model('productsmodel');
		$data["item"] = $this->productsmodel->get_product($product_id, "id_Products, Name_Products, Introduction_Products, Main_Image");        
        if (empty($data["item"])) show_404();
		
		// fetch wishlists
		$data["wishlists"] = $this->wishlistsmodel->get_list($this->session->userdata('user_id'));
		
		$data['original_wishlist_id'] = $wishlist_id;

		$data['token'] = generate_token("move_to_wishlist",20);
		
		// load views
        $this->load->view('website/wishlist/wishlist_move_product_overlay', $data);
	}
	
	function overlay_remove_product( $product_id = NULL, $wishlist_id = NULL ) 
	{
        if ($product_id==NULL) show_404();		
		
		// fetch product data
        $this->load->model('productsmodel');
		$data["item"] = $this->productsmodel->get_product($product_id, "id_Products, Name_Products, Introduction_Products, Main_Image");        
        if (empty($data["item"])) show_404();
		
		// fetch wishlists
		$data["wishlist"] = $this->wishlistsmodel->get_wishlist_info($wishlist_id, $this->session->userdata('user_id'));
		if (empty($data["item"])) show_404();
		
		$data['token'] = generate_token("remove_from_wishlist",20);
		
		// load views
        $this->load->view('website/wishlist/wishlist_delete_product_overlay', $data);
	}
	
	function overlay_enquire() 
	{
		$products = $this->input->post("products",TRUE);
		
		if (empty($products)){
			echo '
				<h1>No products</h1><p>Please select one or more products for your enquiry</p>
				<br /><br />
				<a class="button cancel" href="#">CLOSE</a>					
			';
			return;
		}

		// fetch product data
        $this->load->model('productsmodel');		
		$data['products'] = $this->productsmodel->get_products_by_id($products);
		$data['token'] = generate_token("wishlist_enquire",20);
		
		$user = $this->usermodel->get_user_details($this->session->userdata('user_id'));
		$data["sender_email"] = $user['Email_UsersDetails'];
		
		// load views
		$this->load->view('website/wishlist/wishlist_enquire_overlay', $data);
	}
	
	function submit_enquiry(){
				
		 if(validate_token("wishlist_enquire",$this->input->post("token",TRUE))){
			 //validate email
			 $this->send_enquiry_email($this->input->post("products",TRUE), $this->input->post("message",TRUE));
			 // show success
			 $user=$this->usermodel->get_user_details($this->session->userdata('user_id'));
			 echo "<h2>".sprintf(($this->lang->line('EnquirySuccess1')), $user['FirstName_UsersDetails'])."</h2><p>".sprintf(($this->lang->line('EnquirySuccess2')), $user['Email_UsersDetails'])."</p>";
		 } else {	 
			 // show error
			 echo "<h2>".$this->lang->line('EnquiryError1')."</h2><p>".$this->lang->line('EnquiryError2')."</p>";			 
		}
	} 

	private function send_enquiry_email($products=array(), $message=""){	
		 // gather the data
		 $this->load->model('productsmodel');
		 $data['user'] = $this->usermodel->get_user_details($this->session->userdata('user_id'));
		 $data['products'] = $this->productsmodel->get_products_by_id($products);
		 $data['message'] = $message;

		// load email template
		$email_body = $this->load->view('templates/emails/wishlist_enquiry'.INSTANCE_FILE_SUFFIX, $data, TRUE);
		
		// send the email
		$this->load->helper('send_email');     
		send_email(EMAIL_SENDER, 
					EMAIL_SENDER_NAME, 
					PRODUCT_ENQUIRY_RECIPIENT, 
					"Product enquiry from ".$data['user']['FirstName_UsersDetails']." ".$data['user']['LastName_UsersDetails'], 
					$email_body);		
	}
	
	function overlay_delete_wishlist($wishlist_id=NULL) 
	{
		if ($wishlist_id==NULL) show_404();
		
		$data["wishlist"] = $this->wishlistsmodel->get_wishlist_info($wishlist_id, $this->session->userdata('user_id')); 
			
		if (empty($data["wishlist"])) show_404();

		$list = $this->wishlistsmodel->get_list($this->session->userdata('user_id'), $wishlist_id); 
		$data["products"] = isset($list[0]['items']) ? $list[0]['items'] : array();
					
		$data['token'] = generate_token("delete_wishlist",20);
		
		$this->load->view('website/wishlist/wishlist_delete_list_overlay', $data);
	}
	
	function overlay_create_new_wishlist( ) 
	{
		$data['token_create'] = generate_token("create_wishlist",20);
		$this->load->view('website/wishlist/wishlist_new_list_overlay', $data);
	}
	
    function create()
    {
		
	    if(validate_token("create_wishlist",$this->input->post("token",TRUE))){	
			 
			$wishlist_name = trim($this->input->post("wishlist_name"));
			$user_id = $this->session->userdata('user_id');
			
			if ($wishlist_name != "") 
			{
				$this->wishlistsmodel->create_new_wishlist($wishlist_name, $user_id);	
			}

		}
		redirect( 'wishlist', 'refresh');
    }


    // process form inputs and perform database operations
    private function _run_wishlist_manager()
    {

        if($this->input->post("wishlist_action") !== FALSE){  

			if(validate_token("wishlist_manager",$this->input->post("token",TRUE))){	
	
				switch($this->input->post("wishlist_action")){
	
					case 'create_wishlist':
	
						$this->_create_wishlist();
	
					break;
					case 'delete_wishlist':
	
						$this->_delete_wishlist();
	
					break;
					case 'move_product':
	
						$this->_move_product();
	
					break;         
					case 'add_product':
	
						$this->_add_product();
	
					break;
					case 'remove_product':
	
						$this->_remove_product();
	
					break;
	
				}
			
			}

        }

    }

    private function _create_wishlist(){

        if(trim($this->input->post("wishlist_name", TRUE)) != ''){

            $user_id = $this->session->userdata('user_id');
            $wishlist_id = $this->wishlistsmodel->create_new_wishlist($this->input->post("wishlist_name", TRUE), $user_id);    

            if ($this->input->post("product_id", TRUE) != FALSE){
                $this->wishlistsmodel->add_product($this->input->post("product_id", TRUE), $wishlist_id);
            }
            return true;
        }
        return false;

    }
    function delete_wishlist(){

		if(validate_token("delete_wishlist",$this->input->post("token",TRUE)))
		{	
        	$this->wishlistsmodel->delete_wishlist($this->input->post("wishlist_id", TRUE), $this->session->userdata('user_id'));    
		}
	
		redirect('wishlist');
		
    }
    private function _move_product(){

        $user_id = $this->session->userdata('user_id');     
       
       if (($this->input->post("wishlist_name", TRUE) != FALSE) && (trim($this->input->post("wishlist_name", TRUE)) != '')){
            $to_wishlist_id = $this->wishlistsmodel->create_new_wishlist($this->input->post("wishlist_name", TRUE), $user_id);    
       } else if ($this->input->post("to_wishlist_id", TRUE) != FALSE){
            $to_wishlist_id = $this->input->post("to_wishlist_id");
       } else {
            return false;
       }
       $this->wishlistsmodel->move_product($this->input->post("product_id"), $this->input->post("from_wishlist_id"), $to_wishlist_id, $this->input->post("user_id"));

    } 
    private function _remove_product(){
       
        $user_id = $this->session->userdata('user_id');
        $this->wishlistsmodel->remove_product($this->input->post("product_id"), $this->input->post("from_wishlist_id"), $user_id);

    }
    //private function _add_product(){
       
    //    $user_id = $this->session->userdata('user_id');
    //    $this->wishlistsmodel->add_product($this->input->post("product_id"), $this->input->post("to_wishlist_id"), $user_id);

    //}

}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */